package cn.attackme.myuploader.controller;

import cn.attackme.myuploader.model.Icon;
import cn.attackme.myuploader.service.IconServiceImpl;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;


/**
 * 图标文件上传
 */
@RestController
@RequestMapping("/wf/file/")
@CrossOrigin
public class IconController {
    @Autowired
    private IconServiceImpl iconServiceImpl;

    @ResponseBody
    @PostMapping(value = "/uploadIcon")
    @ApiOperation(value = "上传单个图标文件",produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiImplicitParam(paramType = "file", name = "file", value = "文件", dataType = "MultipartFile")
    public void upload(String md5, @RequestParam(value = "file",required = true) MultipartFile file) throws IOException {

        if (file.isEmpty()) {
            System.out.println("请选择图片");
        }

        String fileName = file.getOriginalFilename();  // 文件名
        String originName = fileName.substring(0,fileName.lastIndexOf("."));  // 上传图标的原始名称 如安全隐私，版权

        byte[] fileByte = null;
        try {
            fileByte = file.getBytes();
        } catch (IOException e) {
            System.out.println("图像文件转码失败！");
        }
        String iconBase64= Base64.getEncoder().encodeToString(fileByte);

        //图片信息上传到数据库
        iconServiceImpl.upload(originName, md5,iconBase64);
    }

    @PostMapping("/uploadIcons")
    @ApiOperation(value = "上传多个图标文件")
    @ApiImplicitParam(paramType = "file", name = "file", value = "文件", dataType = "MultipartFile")
    public void uploadFiles(String md5, @RequestParam("file") MultipartFile[] files) throws IOException {

        for(MultipartFile file:files){
            if (file.isEmpty()) {
                System.out.println("请选择图片");
            }
            String fileName = file.getOriginalFilename();  // 文件名
            String originName = fileName.substring(0,fileName.lastIndexOf("."));  // 上传图标的原始名称 如安全隐私，版权

            byte[] fileByte = null;
            try {
                fileByte = file.getBytes();
            } catch (IOException e) {
                System.out.println("图像文件转码失败！");
            }
            String iconBase64= Base64.getEncoder().encodeToString(fileByte);

            //图片信息上传到数据库
            iconServiceImpl.upload(originName, md5,iconBase64);
        }
    }

    @GetMapping("/getIconByName")
    @ApiOperation(value = "根据名称获取图标信息")
    public String getIconPath(String name) throws IOException {

        Icon icon = iconServiceImpl.getIconByName(name);
        String iconBase64 = icon.getIconImage();
        return iconBase64;
    }

    @GetMapping("/getIconById")
    @ApiOperation(value = "根据id获取图标信息")
    @ResponseBody
    public Icon getIconById(String id) throws IOException {

        Icon icon = iconServiceImpl.getById(id);
        String iconBase64 = icon.getIconImage();

        /*//base64解码写出到文件测试
        String iconName = icon.getName();
        String targetPath = "D:\\gpm\\iconLibray\\out\\" + iconName + ".jpg";
        File file=new File(targetPath);
        String path=targetPath.substring(0,targetPath.lastIndexOf("\\"));
        if(!file.exists()){
            new File(path).mkdirs();
        }
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(file);
            int len=0;
            byte[] base64decodedBytes = Base64.getDecoder().decode(iconBase64);
            len = base64decodedBytes.length;
            fos.write(base64decodedBytes,0,len);

            fos.flush();
        } catch(Exception e){
            e.printStackTrace();
        }finally {
            if(null != fos){
                try{
                    fos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }*/
        return icon;
    }

    @DeleteMapping("/deleteIconById")
    @ApiOperation(value = "根据id删除图标信息")
    public void deleteById(String id) throws IOException {
        iconServiceImpl.deleteById(id);
    }

    @PutMapping("/updateIcon")
    @ApiOperation(value = "根据id更新图标信息")
    public void updateIcon(String id,String name) throws IOException {
        Icon icon = new Icon();
        icon.setId(id);
        icon.setName(name);
        iconServiceImpl.updateIcon(icon);
    }

    @GetMapping("/getByIcon")
    @ApiOperation(value = "根据一个或多个属性获取图标")
    @ResponseBody
    public Icon getByIcon(String md5,String name) throws IOException {
        Icon icon = new Icon();
        icon.setName(name);
        icon.setMd5(md5);
        return iconServiceImpl.getByIcon(icon);
    }


}
